const koa = require('koa')
const app = new koa();
const mongoose = require('mongoose')
const views = require('koa-views')
const { resolve } = require('path')
const { connect, initSchemas } = require('./database/init')
const router = require('./routes') //引入路由

;(async () => {
	await connect()
	initSchemas()
	// const Movie = mongoose.model('Movie')
	// const movies = await Movie.find({})
	// console.log(movies)
	//require('./tasks/movie') //引入第一份列表脚本 
	require('./tasks/api') //引入第二份脚本 
})()

//添加路由
app
	.use(router.routes())
	.use(router.allowedMethods()) //允许路由方法

//自动识别pug的模板引擎
app.use(views(resolve(__dirname,'../views'),{
	extension:'pug'
}))
app.use(async (ctx, next) =>{
	//ctx.type ="text/html;charset=utf-8"
	await ctx.render('index', {
		you: 'Luke',
		me : 'Andy2u'
	})
})

app.listen(4455);